package han.chensing.latos.game.elements.notes;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import han.chensing.latos.res.Res;
import han.chensing.latos.game.util.MathUtil;

public class HoldNote extends Note{

    protected static final float judgePardonInitial=0.15f;

    protected TextureRegion body;
    protected TextureRegion end;
    protected HitStatue tempHitStatue;
    protected Integer bodyLength;
    protected float endHitTime;
    protected float judgePardon;
    protected float lastJudgeTime;
    protected boolean isHolding;
    protected int holdingTouch;
    protected int trueLength;
    protected boolean missed;

    public HoldNote(Line line) {
        super(line,
                Res.getInstance().get("holdNoteHead",TextureRegion.class),
                NoteType.Hold);
        Res res = Res.getInstance();
        this.body= res.get("holdNoteBody",TextureRegion.class);
        this.end= res.get("holdNoteEnd",TextureRegion.class);
        this.judgePardon=0;
        this.isHolding=false;
        this.missed=false;
        this.holdingTouch=-1;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        float distanceHeight = getDistanceHeight();
        /*GameScreen.shapeRenderer.circle(trueX-getWidth()/2,
                trueY-getHeight()/2-Math.min(0,distanceHeight),6);*/
        Line line = getBindLine();
        float speed = line.getNoteSpeed().getSpeed().x;
        float hitLength = (endHitTime - hitTime);
        setBodyLength((int) (hitLength*speed));
        boolean backGoing = isBackGoing();
        int backGoingFix1 = backGoing ? -1 : 1;
        int backGoingFix2 = backGoing ? 1 : 0;
        float rotation = line.getRotation();
        float[] pos1 = MathUtil.getPosOutOfLine(
                getX(),getY(),
                rotation - 90,
                Math.min(0, distanceHeight+backGoingFix2*bodyLength)
                        +backGoingFix2*bodyLength-backGoingFix1 * getHeight() / 2f);
        if (missed)
            batch.setColor(1,1,1,0.6f);
        batch.draw(body,pos1[0],pos1[1],
                getOriginX(),getOriginY(),getWidth(),
                        Math.max(0,
                                backGoing?
                                        (Math.min(
                                                bodyLength,
                                                distanceHeight+bodyLength)+getHeight())
                                        :(Math.min(
                                                bodyLength,
                                                bodyLength+distanceHeight))),
                getScaleX(),getScaleY(), rotation);
        if (missed)
            batch.setColor(1,1,1,1);
        if (distanceHeight<0)
            return;
        super.draw(batch, parentAlpha);
    }

    public void resetJudgePardon(){
        this.judgePardon=judgePardonInitial;
    }

    public float getJudgePardon() {
        return judgePardon;
    }

    public void setJudgePardon(float judgePardon) {
        this.judgePardon = judgePardon;
    }

    public boolean isHolding() {
        return isHolding;
    }

    public void setHolding(boolean holding) {
        isHolding = holding;
    }

    public float getLastJudgeTime() {
        return lastJudgeTime;
    }

    public void setLastJudgeTime(float lastJudgeTime) {
        this.lastJudgeTime = lastJudgeTime;
    }

    public HitStatue getTempHitStatue() {
        return tempHitStatue;
    }

    public void setTempHitStatue(HitStatue tempHitStatue) {
        this.tempHitStatue = tempHitStatue;
    }

    public int getBodyLength() {
        return bodyLength;
    }

    public void setBodyLength(int bodyLength) {
        this.bodyLength = bodyLength;
        this.trueLength= (int) (this.bodyLength+getHeight());
    }

    public int getTrueLength() {
        return trueLength;
    }

    public void setTrueLength(int trueLength) {
        this.trueLength = trueLength;
    }

    public boolean isMissed() {
        return missed;
    }

    public void setMissed(boolean missed) {
        this.missed = missed;
    }

    public float getEndHitTime() {
        return endHitTime;
    }

    public void setEndHitTime(float endHitTime) {
        this.endHitTime = endHitTime;
    }

    public int getHoldingTouch() {
        return holdingTouch;
    }

    public void setHoldingTouch(int holdingTouch) {
        this.holdingTouch = holdingTouch;
    }
}
